package com.example.demo.service.impl;

import com.example.demo.context.BaseContext;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.pojo.PageResult;
import com.example.demo.pojo.dto.GoodsPageDTO;
import com.example.demo.pojo.dto.GoodsQueryDTO;
import com.example.demo.pojo.entity.Goods;
import com.example.demo.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<Goods> list(GoodsQueryDTO goodsQueryDTO) {
        Goods goods = new Goods();
        goods.setName(goodsQueryDTO.getName());
        return goodsMapper.list(goods);
    }

    @Override
    public Goods getGoodsInfo(Long id) {
        return goodsMapper.getById(id);
    }

    @Override
    public void add(Goods goods) {
        goods.setCreateTime(LocalDateTime.now());
        goods.setCreateUser(BaseContext.getCurrentId());
        goodsMapper.add(goods);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        goodsMapper.batchDelete(ids);
    }

    @Override
    public void update(Goods goods) {
        goodsMapper.update(goods);
    }

    @Override
    public PageResult pageQuery(GoodsPageDTO goodsPageDTO) {
        PageHelper.startPage(goodsPageDTO.getPage(), goodsPageDTO.getPageSize());
        Page<Goods> page=goodsMapper.pageQuery(goodsPageDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
}
